package com.pb.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.pb.entity.User;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = -1783819998312712862L;

	private User user;

	public String register() throws Exception {
		// 完成注册业务
		System.out.println("完成注册业务");
		// addActionError("用户名或密码错误");//添加Action错误
		if (this.hasErrors()) {
			return INPUT;
		}
		return SUCCESS;
	}

	@Override
	public void validate() {
		System.out.println("Register");
		// 验证用户名
		if (null == user.getUsername() || "".equals(user.getUsername())) {
			this.addFieldError("user.username", "用户名不能为空（validate方法）");
		}
		// 验证密码
		if (null == user.getPassword() || "".equals(user.getPassword())) {
			this.addFieldError("user.password", "密码不能为空（validate方法）");
		}
	}

	public void validateRegister() {
		System.out.println("validateRegister");
		// 验证年龄
		if (user.getAge() < 0 || user.getAge() > 150) {
			this.addFieldError("user.age", "年龄需要在0-150之间（validate方法）");
		}
	}

	/**
	 * 获取 user
	 * 
	 * @return user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * 设置 user
	 * 
	 * @param user
	 *            user
	 */
	public void setUser(User user) {
		this.user = user;
	}

}
